//
//  ZYZMainTabBar.swift
//  MyWeiBo
//
//  Created by 牛牛很牛 on 15/12/15.
//  Copyright © 2015年 曾勇召. All rights reserved.
//

import UIKit

class ZYZMainTabBar: UITabBar {

    let count = 5

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.bounds.width / CGFloat(count)
        
        //最左边的Frame
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        
        //便利所有的子控件
        var index = 0
        for view in subviews{
        //判断类型, UITabBarButton继承UIControl，过滤掉要加入的button
            if view is UIControl && !(view is UIButton){
                view.frame = CGRectOffset(frame, width*CGFloat(index), 0)
                index += index == 1 ? 2 : 1
            }
        }
        addButton.frame = CGRectOffset(frame, width*2.0, 0)
    }
    
    //懒加载  添加按钮
     lazy  var addButton : UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        self.addSubview(button)
       return button
    }()

    
    
}
